package com.bjsdzk.collector.rpc;

import java.util.Base64;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Service;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.iot.model.v20180120.PubRequest;
import com.aliyuncs.iot.model.v20180120.PubResponse;
import com.aliyuncs.iot.model.v20180120.RRpcRequest;
import com.aliyuncs.iot.model.v20180120.RRpcResponse;
import com.bjsdzk.backend.rpc.XiYuService;
import com.bjsdzk.backend.rpc.after.AliIotAfter;
import com.bjsdzk.collector.otherFlatform.AliIotRK;
import com.bjsdzk.collector.utils.Utils;
import com.google.gson.Gson;


@Service(
		version = "1.0.0",
        interfaceClass = AliIotAfter.class
)
@Component
public class AliIotServer implements AliIotAfter{
	
	private static Logger logger = LoggerFactory.getLogger(AliIotServer.class);
	@Override
	public boolean cmd(String msg) {
		logger.info("rk cmd"+msg);
		try {
		JSONObject params = new JSONObject(msg);
		 String deviceType=params.getString("deviceType");
		 String deviceName=params.getString("deviceName");
		 String pk = AliIotRK.productKey.get(deviceType);
		 String topic="";
		 String par="";
		 if(StringUtils.equals("rk", deviceType)) {
			 String id=params.getString("id");
			 String operation=params.getString("operation");
			 String value=params.getString("value");
			 
			 topic="/" + pk + "/" + deviceName + "/user/get";
			 JSONObject json = new JSONObject();
				json.put("id", id);
				json.put("cmd", operation);// silence
				if (StringUtils.equals("revise", operation)) {
					json.put("data", new JSONObject(value));
				}
				par=json.toString();
		 }else if(StringUtils.equals("ss130", deviceType)) {
//			 topic="/" + pk + "/" + deviceName + "/user/get";
			 topic= "/sys/"+pk+"/"+deviceName+"/thing/model/down_raw";
			 topic= "/"+pk+"/"+deviceName+"/thing/event/property/post_reply";
			 String c="14B1"+Utils.bytesToHex("867900047083657".getBytes())+"00"+"0003";
			 par="FE0014"+Utils.checkNumSecondAll(c);
		 }

			System.out.println(topic);
			System.out.println(par);
			PubRequest request = new PubRequest();
			request.setQos(0);
			// 设置发布消息的topic
			request.setTopicFullName(topic);
			request.setProductKey(pk);
			// 设置消息的内容，一定要用base64编码，否则乱码
			

			request.setMessageContent(Base64.getEncoder().encodeToString(par.getBytes()));

			PubResponse response = AliIotRK.client.getAcsResponse(request);
			logger.info("rk cmd  pk:"+pk+"response:"+ new Gson().toJson(response));
			return response.getSuccess();
		} catch (Exception e) {
			logger.info("rk cmd error"+e.getMessage());
		}
		return false;
	}
	
	public String rrpcCmd(String deviceType, String deviceName) {
		String pk = AliIotRK.productKey.get(deviceType);
		String topic = "";
		String par = "";
		if (StringUtils.equals("rk", deviceType)) {
		} else if (StringUtils.equals("ss130", deviceType)) {
			topic = "/" + pk + "/" + deviceName + "/user/get";
			topic = "/sys/" + pk + "/" + deviceName + "/thing/model/down_raw";
		    topic= "/"+pk+"/"+deviceName+"/thing/event/property/post_reply";
			String c = "14B1" + Utils.bytesToHex("867900047083657".getBytes()) + "00" + "0003";
			par = "FE0014" + Utils.checkNumSecondAll(c);
			System.out.println(topic);
			System.out.println(par);
		}
		RRpcRequest request = new RRpcRequest();
		request.setTopic(topic);
		request.setRequestBase64Byte(Base64.getEncoder().encodeToString(par.getBytes()));
		request.setActionName("RRpc");
		request.setProductKey(pk);
		request.setDeviceName(deviceName);
		request.setTimeout(8000);
		try {
			RRpcResponse response = AliIotRK.client.getAcsResponse(request);
			System.out.println(new Gson().toJson(response));
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			System.out.println("ErrCode:" + e.getErrCode());
			System.out.println("ErrMsg:" + e.getErrMsg());
			System.out.println("RequestId:" + e.getRequestId());
		}
			return "";
	}
}
